import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv, UserConfigExport, ConfigEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import {viteMockServe} from 'vite-plugin-mock'

export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  let env = loadEnv(mode, process.cwd());
  return {
    plugins: [
      vue(),
      viteMockServe({
        //mockPath: 'mock',
        enable: true, //保证开发阶段可以使用mock接口
        logger: true,
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    // 配置代理跨域.
    server:{
      proxy: {
        [env.VITE_APP_BASE_API]: {
          // 获取数据的服务器地址设置;
          target: env.VITE_SERVER_URL,
          // 是否跨域
          changeOrigin: true,
          // 路经重写.
          rewrite:(path) => path.replace(/^\/api/, ''),
        }
      }
    }  
  }
}